package com.lcj.bff.driver.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.lcj.bff.driver.controller.form.*;
import com.lcj.bff.driver.feign.DriverServiceApi;
import com.lcj.bff.driver.feign.OrderServiceApi;
import com.lcj.bff.driver.service.DriverService;
import com.lcj.common.util.CosUtil;
import com.lcj.common.util.Result;
import com.lcj.common.util.SendSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author : lcj
 * @create 2022/11/3 21:52
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverServiceApi driverServiceApi;

    @Resource
    private OrderServiceApi orderServiceApi;

    @Resource
    private CosUtil cosUtil;

    @Resource
    private SendSmsUtil sendSmsUtil;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    @LcnTransaction
    public Result registerNewDriver(RegisterNewDriverForm form) {
        Result result = driverServiceApi.registerNewDriver(form);
        return result;
    }

    @Override
    @LcnTransaction
    public int updateDriverAuth(UpdateDriverAuthForm form) {
        Result res = driverServiceApi.updateDriverAuth(form);
        int rows = Convert.toInt(res.get("rows"));
        return rows;
    }

    @Override
    @LcnTransaction
    public String createDriverFaceModel(CreateDriverFaceModelForm form) {
        Result res = driverServiceApi.createDriverFaceModel(form);
        String result = MapUtil.getStr(res, "result");
        return result;
    }

    public HashMap login(@RequestBody @Valid LoginForm form) {
        Result res = driverServiceApi.login(form);
        HashMap map = (HashMap)res.get("result");
        return map;
    }

    @Override
    public HashMap searchDriverBaseInfo(DriverIdForm form) {
        //查司机基本信息
        Result r = driverServiceApi.searchDriverBaseInfo(form);
        HashMap map = (HashMap) r.get("result");
        JSONObject summary = new JSONObject();
        //查询总订单量
        Result result = orderServiceApi.searchDriverOrderWeekNumber(form);
        String totalOrder = (String)result.get("result");
        summary.put("totalOrder", totalOrder);
        summary.put("level", Integer.parseInt(totalOrder)/50);
        //查本周订单数
        result = orderServiceApi.searchDriverOrderWeekNumber(form);
        String orderWeekNumber = (String)result.get("result");
        summary.put("weekOrder", orderWeekNumber);
        //查申述数
        result = orderServiceApi.searchDriverAppealNumber(form);
        String appeal = (String)result.get("result");
        summary.put("appeal", appeal);
        //查本周顾客好评数
        result = orderServiceApi.searchWeekGoodCommentNumber(form);
        String weekComment = (String)result.get("result");
        summary.put("weekComment", weekComment);

        //替换summary
        map.put("summary", summary);
        return map;
    }

    @Override
    public HashMap searchWorkbenchData(long driverId) {
        //查询司机当天业务数据
        DriverIdForm form = new DriverIdForm(driverId);
        Result r = orderServiceApi.searchDriverTodayBusinessData(form);
        HashMap business = (HashMap) r.get("result");

        //查询司机的设置
        r = driverServiceApi.searchDriverSettings(form);
        HashMap settings = (HashMap) r.get("result");
        HashMap result = new HashMap(2) {{
            put("business", business);
            put("settings", settings);
        }};
        return result;
    }

    @Override
    public HashMap searchDriverAuth(DriverIdForm form) {
        Result r = driverServiceApi.searchDriverAuth(form);
        HashMap map = (HashMap) r.get("result");
        //获取私有读写文件的临时URL地址
        String idcardFrontUrl = cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "idcardFront"));
        String idcardBackUrl = cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "idcardBack"));
        String idcardHoldingUrl =  cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "idcardHolding"));
        String drcardFrontUrl = cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "drcardFront"));
        String drcardBackUrl = cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "drcardBack"));
        String drcardHoldingUrl = cosUtil.getPrivateFileUrl(MapUtil.getStr(map, "drcardHolding"));
        map.put("idcardFrontUrl", idcardFrontUrl);
        map.put("idcardBackUrl", idcardBackUrl);
        map.put("idcardHoldingUrl", idcardHoldingUrl);
        map.put("drcardFrontUrl", drcardFrontUrl);
        map.put("drcardBackUrl", drcardBackUrl);
        map.put("drcardHoldingUrl", drcardHoldingUrl);
        return map;
    }

    @Override
    public Result updateDriverSettings(DriverSettingsForm form) {
        Result r = driverServiceApi.updateDriverSettings(form);
        return r;
    }

    @Override
    public Result recharge(DriverRechargeForm form) {
        Result r = driverServiceApi.recharge(form);
        return r;
    }

    @Override
    public Map<String, Object> searchInfo(DriverWalletInfoForm form) {
        Map<String, Object> r = driverServiceApi.searchInfo(form);
        return r;
    }

    @Override
    public Result updatePhone(UpdatePhoneForm form) {
        //验证验证码是否正确
        String smsCode = form.getSmsCode();
        String code = redisTemplate.opsForValue().get("smsCode_" + form.getPhone());
        if(code == null) {
            return Result.error("验证码已过期！请重新发送");
        }
        if(!code.equals(smsCode)) {
            return Result.error("验证码错误！");
        }
        Result r = driverServiceApi.updatePhone(form);
        redisTemplate.delete("smsCode_" + form.getPhone());
        return r;
    }

    @Override
    public Result sendSmsCode(DriverSendSmsForm form) {
        int code = RandomUtil.randomInt(100000, 999999);
        try {
            redisTemplate.opsForValue().set("smsCode_" + form.getPhone(), code + "", 5, TimeUnit.MINUTES);
            sendSmsUtil.sendSms(form.getPhone(), code + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @Override
    public Result updateDriverHome(UpdateDriverHomeForm form) {
        Result r =  driverServiceApi.updateDriverHome(form);
        return r;
    }

    @Override
    public String vertificateDriverFace(CreateDriverFaceModelForm form) {
        Result res = driverServiceApi.vertificateDriverFace(form);
        String result = MapUtil.getStr(res, "result");
        return result;
    }

    @Override
    public HashMap searchCanStartWork(SearchCanStartWorkForm form) {
        Result r = driverServiceApi.searchCanStartWork(form);
        HashMap map= (HashMap) r.get("result");
        return map;
    }

}
